<template>
  <div class="login-container">
    <img class="logo-view" style="cursor: pointer;" src="../../assets/login/logo-login.png" />
    <el-form ref="loginForm" 
    :model="loginForm" 
    :rules="loginRules" 
    class="login-form" 
    autocomplete="on" 
    label-position="left">

      <div class="title-container">
        探程后台管理系统
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入登录账户"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入登录密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;margin-top:30px;background: #30ab74;border-color:#30ab74" 
         @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { removeToken } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入登录账户'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入登录密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: 'admin123',
        loginType:'boss'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    removeToken()
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then((res) => {
              this.loading = false
              if(res.success){
                localStorage.setItem('menuKey',1)
                this.$router.push({ path: '/home', query: this.otherQuery })
              }else{
                this.$message.error(res.message||"操作失败");
              }
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
.login-container {
  background-image: url('../../assets/login/login-bg.jpg');
  background-position: top;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 36px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
    }
  }
  .el-form-item {
    background: #f5f5f5;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  position: relative;
  min-width:1000px;
  height:100vh;
  min-height:1000px;
  .logo-view{
    height:180px;
    position: absolute;
    left:100px;
    top:50px;
  }
  .center-login{
    width:500px;
    height:100px;
    text-align: center;
    margin:0 auto;
    position: absolute;
    left:50%;
    margin-left:-250px;
    top:160px;
    display: flex;
    justify-content: space-between;
    img{
      height:63px;
      width:228px;
    }
  }
  .login-form {
    position: absolute;
    width: 360px;
    height:400px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
    left:50%;
    margin-left:-200px;
    top:50%;
    margin-top:-300px;
    padding:30px;
    border-radius:6px;
  }

  .tip-view{
    font-size:14px;
    color:#565656;
    line-height:25px;
    position: relative;
    .down{
      position: absolute;
      font-size:14px;
      right:0;
      color:#0777df;
      font-weight: bold;
      bottom:0;
      text-decoration:underline
    }
  }
  .svg-container {
    padding: 0 5px 0 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    font-size: 20px;
    color: #333;
    margin: 0px auto 40px auto;
    text-align: left;
    font-weight: bold;
    border-bottom:1px solid #ddd;
    line-height: 50px;
  }
  .title-container::after{
    content: '';
    width:50px;
    height:2px;
    position: absolute;
    bottom:0;
    left:0;
    background-color: #565656;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  .el-button--primary {
      color: #FFFFFF;
      background-color: #30ab74 !important;
      border-color: #30ab74 !important;
  }
  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
